﻿using System.Linq;
using System.Text;
using ArtheaEngine;

namespace ArtheaServer.Commands
{
    public class HelpCommand : Command
    {
        public HelpCommand() : base("help", "Finds help for a subject.")
        {
        }


        public override void Execute(Character ch, Argument argument)
        {
            if (argument.IsEmpty())
            {
                ch.WriteLine("Show help on what?");
                return;
            }

            bool found = false;

            foreach (Help h in World.DataContext.Helps)
            {
                if (!h.Keywords.HasWord(argument.ToString()))
                    continue;

                found = true;

                ch.WriteLine("Keywords: {0}", h.Keywords);
                ch.WriteLine(h.Text);

                IQueryable<ExtraHelp> also = from a in World.DataContext.ExtraHelps where a.HelpId == h.HelpId select a;

                if (also.Count() > 0)
                {
                    var buf = new StringBuilder();
                    ch.Write("See also: ");
                    foreach (ExtraHelp a in also)
                    {
                        buf.Append(", ").Append(a.Extra.Keywords);
                    }
                    buf.Remove(0, 2);
                    ch.WriteLine(buf);
                }
            }

            if (!found)
            {
                ch.WriteLine("No help for that topic");
                return;
            }
        }
    }
}